home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / redakcyjne / programy / FEBE (Firefox Environment Backup Extension) 5.0 / febe-5.0-fx.xpi / chrome / febe.jar / content / febeUDBu.js < prev    next >
Text File  |  2007-06-10  |  17KB  |  451 lines

  1. // author: Chuck Baker
  2. // contact: firefox@customsoftwareconsult.com
  3. // FEBE User-defined backup routines
  4.  
  5. var febeUDBuList = [];
  6. var febeUDBuAction = new Number;    // 1=New, 2=Edit, 3=Delete
  7. var febeUDBuTrxPending = new Boolean(false);
  8.  
  9. function febeUDBuObj(Label, Type, Description, Path, Include){
  10.     this.Label = new String(Label);
  11.     this.Type = new Number(Type);
  12.     this.Description = new String(Description);
  13.     this.Path = new String(Path); 
  14.     this.Include = new Boolean(Include); 
  15.     return true;
  16. }//febeUDBuObj
  17.  
  18. function febeLoadUDBu(){
  19.     var febeUDBuWin = window.openDialog("chrome://febe/content/febeUDBu.xul", febeMsg[147], "chrome,alwaysRaised,centerscreen,resizable");    
  20.     return true;
  21. }//febeLoadUDBu()
  22.  
  23. function febeNewUDBu(){
  24.     febeUDBuDisableAll();
  25.     document.getElementById("febeUDBuCaption").value = febeMsg[136];
  26.     febeUDBuAction = 1;
  27.     febeUDBuEnableNew();
  28.     return true;
  29. }//febeNewUDBu()
  30.  
  31. function febeEditUDBu(){
  32.     febeUDBuDisableAll();
  33.     document.getElementById("febeUDBuCaption").value = febeMsg[137];
  34.     febeUDBuAction = 2;
  35.     febeUDBuPopulateListbox();
  36.     return true;
  37. }//febeEditUDBu()
  38.  
  39. function febeDeleteUDBu(){
  40.     febeUDBuDisableAll();
  41.     document.getElementById("febeUDBuCaption").value = febeMsg[138];
  42.     febeUDBuAction = 3;
  43.     febeUDBuPopulateListbox();
  44.     return true;
  45. }//febeDeleteUDBu()
  46.  
  47. function febeSelUDBu(){
  48.     var dd = document.getElementById("febeUDBuType");
  49.     if(dd.selectedIndex == -1){
  50.         var tmp = febeMsg[165];
  51.         febeAlert(tmp);
  52.         return false;
  53.     }//if
  54.     var type = dd.selectedIndex;
  55.     switch(type){
  56.         case 0:        // File
  57.             febeSelUDBuFile();
  58.             break;
  59.         case 1:        // Folder
  60.             febeSelUDBuFolder();
  61.             break;
  62.     }//switch
  63.     return true;
  64. }//febeSelUDBu(){
  65.  
  66. function febeSelUDBuFile(){
  67.   // Select a file to backup
  68.     const nsIFilePicker = Components.interfaces.nsIFilePicker;
  69.     var fp = Components.classes["@mozilla.org/filepicker;1"]
  70.                     .createInstance(nsIFilePicker);
  71.     fp.init(window, febeMsg[133], nsIFilePicker.modeOpen);
  72.     fp.appendFilters(nsIFilePicker.filterAll);
  73.     
  74.     // Set the default directory to the current profile directory
  75.     febeProfDir = Components.classes["@mozilla.org/file/directory_service;1"]
  76.         .getService(Components.interfaces.nsIProperties)
  77.         .get("ProfD", Components.interfaces.nsIFile);
  78.     
  79.     fp.displayDirectory = febeProfDir;
  80.  
  81.     var rv = fp.show();
  82.     if (rv == nsIFilePicker.returnOK){
  83.         rv = fp.file;
  84.         document.getElementById("febeUDBuSource").value = rv.path;
  85.         return true;
  86.     }//if
  87.     return false;
  88. }//febeSelUDBuFile()
  89.  
  90. function febeSelUDBuFolder(){
  91.   // Select a folder to backup
  92.     const nsIFilePicker = Components.interfaces.nsIFilePicker;
  93.     var fp = Components.classes["@mozilla.org/filepicker;1"]
  94.              .createInstance(nsIFilePicker);
  95.     fp.init(window, febeMsg[159], nsIFilePicker.modeGetFolder);
  96.     fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterText);
  97.  
  98.     // Set the default directory to the current profile directory
  99.     febeProfDir = Components.classes["@mozilla.org/file/directory_service;1"]
  100.         .getService(Components.interfaces.nsIProperties)
  101.         .get("ProfD", Components.interfaces.nsIFile);
  102.     
  103.     fp.displayDirectory = febeProfDir;
  104.  
  105.     var rv = fp.show();
  106.     if (rv == nsIFilePicker.returnOK){
  107.         rv = fp.file;
  108.         document.getElementById("febeUDBuSource").value = rv.path;
  109.         return true;
  110.     }//if
  111.     return false;
  112. }//febeSelUDBuFolder()
  113.  
  114. function febeUDBuInit(){
  115.     febeSetMsgs();
  116.   // Get data from file - create template if needed
  117.     var febeDataFile = Components.classes["@mozilla.org/file/directory_service;1"]
  118.                  .getService(Components.interfaces.nsIProperties)
  119.                  .get("ProfD", Components.interfaces.nsIFile);
  120.     febeDataFile.append(FEBEDATAFILE);
  121.     if(!febeDataFile.exists()){
  122.       // Create the template
  123.         var dataFile = Components.classes["@mozilla.org/network/file-output-stream;1"]
  124.             .createInstance(Components.interfaces.nsIFileOutputStream);
  125.         dataFile.init(febeDataFile, 0x02 | 0x08 | 0x20, 0755, 0); // write, create, truncate
  126.         
  127.         var charset = "UTF-8";
  128.         var os = Components.classes["@mozilla.org/intl/converter-output-stream;1"]
  129.              .createInstance(Components.interfaces.nsIConverterOutputStream);
  130.  
  131.         os.init(dataFile, charset, 4096, 0x0000);
  132.         os.writeString(febeMsg[182]+",0,"+febeMsg[181]+","+febeDataFile.path+",true\n");
  133.     
  134.         os.close();
  135.     }//if
  136.     
  137.   // Read the data
  138.     var dataFile = Components.classes["@mozilla.org/file/local;1"]
  139.                      .createInstance(Components.interfaces.nsILocalFile);
  140.     dataFile.initWithPath(febeDataFile.path);
  141.  
  142.     // open an input stream from file
  143.     var istream = Components.classes["@mozilla.org/network/file-input-stream;1"]
  144.                         .createInstance(Components.interfaces.nsIFileInputStream);
  145.     istream.init(dataFile, 0x01, 0444, 0);
  146.     istream.QueryInterface(Components.interfaces.nsILineInputStream);
  147.     
  148.     var cis = Components.classes["@mozilla.org/intl/converter-input-stream;1"]
  149.                         .createInstance(Components.interfaces.nsIConverterInputStream);
  150.     
  151.     var charset = "UTF-8";
  152.     cis.init(istream,charset, 1024, Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
  153.     var lis = cis.QueryInterface(Components.interfaces.nsIUnicharLineInputStream);
  154.  
  155.     // read lines into array and parse into individual fields
  156.     febeUDBuList = [];
  157.     var line = {}, hasmore;
  158.     do {
  159.         hasmore = lis.readLine(line);
  160.         var febeUDBuItem = new febeUDBuObj; 
  161.         var delimeter = ",";
  162.         var tmp = line.value;
  163.         
  164.         var p = tmp.indexOf(delimeter);
  165.         febeUDBuItem.Label = tmp.slice(0,p);
  166.         tmp = tmp.slice(p+1);
  167.         
  168.         p = tmp.indexOf(delimeter);
  169.         febeUDBuItem.Type = tmp.slice(0,p);
  170.         tmp = tmp.slice(p+1);
  171.         
  172.         p = tmp.indexOf(delimeter);
  173.         febeUDBuItem.Description = tmp.slice(0,p);
  174.         tmp = tmp.slice(p+1);
  175.         
  176.         p = tmp.indexOf(delimeter);
  177.         febeUDBuItem.Path = tmp.slice(0,p);
  178.         febeUDBuItem.Include = tmp.slice(p+1);
  179.         febeUDBuList[febeUDBuItem.Label] = febeUDBuItem;
  180.     } while(hasmore);
  181.  
  182.     istream.close();
  183.     cis.close();
  184.     try {
  185.         document.documentElement.getButton("extra2").disabled = true;
  186.     }catch(e){;;}
  187.     return true;
  188. }//febeUDBuInit()
  189.  
  190. function febeUDBuWrite(){
  191.   // Get data from file - create template if needed
  192.     var febeDataFile = Components.classes["@mozilla.org/file/directory_service;1"]
  193.                  .getService(Components.interfaces.nsIProperties)
  194.                  .get("ProfD", Components.interfaces.nsIFile);
  195.     febeDataFile.append("febeUserDefinedBuData.dat");
  196.     var dataFile = Components.classes["@mozilla.org/network/file-output-stream;1"]
  197.         .createInstance(Components.interfaces.nsIFileOutputStream);
  198.     dataFile.init(febeDataFile, 0x02 | 0x08 | 0x20, 0755, 0); // write, create, truncate
  199.         
  200.     var charset = "UTF-8";
  201.     var os = Components.classes["@mozilla.org/intl/converter-output-stream;1"]
  202.             .createInstance(Components.interfaces.nsIConverterOutputStream);
  203.  
  204.     os.init(dataFile, charset, 4096, 0x0000);
  205.     for(var i in febeUDBuList){
  206.         var febeLabel = febeUDBuList[i].Label;
  207.         var febeType = febeUDBuList[i].Type;
  208.         var febeDesc = febeUDBuList[i].Description;
  209.         var febePath = febeUDBuList[i].Path;
  210.         var febeInclude = febeUDBuList[i].Include;
  211.         var dl = ",";
  212.         var dataString = febeLabel + dl + febeType + dl + febeDesc + dl + febePath+dl + febeInclude +"\n";
  213.         os.writeString(dataString);
  214.     }//for
  215.     os.close();
  216.     return true;
  217. }//febeUDBuWrite()
  218.  
  219. function febeUDBuDisableAll(){
  220.     document.getElementById("febeUDBuCaption").value = febeMsg[134];
  221.     document.getElementById("febeUDBuList").removeAllItems();
  222.     document.getElementById("febeUDBuList").setAttribute("label",febeMsg[135]);
  223.     document.getElementById("febeUDBuList").disabled = true;
  224.     document.getElementById("febeUDBuLabelID").disabled = true;
  225.     document.getElementById("febeUDBuLabel").disabled = true;
  226.     document.getElementById("febeUDBuTypeID").disabled = true;
  227.     document.getElementById("febeUDBuType").disabled = true;
  228.     document.getElementById("febeUDBuDescID").disabled = true;
  229.     document.getElementById("febeUDBuDesc").disabled = true;
  230.     document.getElementById("febeUDBuSourceID").disabled = true;
  231.     document.getElementById("febeUDBuSource").disabled = true;
  232.     document.getElementById("febeUDBuBrowseBtn").disabled = true;
  233.     document.getElementById("febeIncludeInBu_id").disabled = true;
  234.     document.getElementById("febeUDBuSubmitBtn").disabled = true;
  235.     document.getElementById("febeUDBuLabel").value = "";
  236.     document.getElementById("febeUDBuDesc").value = "";
  237.     document.getElementById("febeUDBuSource").value = "";
  238.     document.getElementById("febeIncludeInBu_id").checked = false;
  239.     return true;
  240. }//febeUDBuDisableAll()
  241.  
  242. function febeUDBuEnableNew(){
  243.     //document.getElementById("febeUDBuCaption").value = febeMsg[136];
  244.     document.getElementById("febeUDBuList").removeAllItems();
  245.     document.getElementById("febeUDBuList").setAttribute("label",febeMsg[135]);
  246.     document.getElementById("febeUDBuList").disabled = true;
  247.     document.getElementById("febeUDBuLabelID").disabled = false;
  248.     document.getElementById("febeUDBuLabel").disabled = false;
  249.     document.getElementById("febeUDBuTypeID").disabled = false;
  250.     document.getElementById("febeUDBuType").disabled = false;
  251.     document.getElementById("febeUDBuDescID").disabled = false;
  252.     document.getElementById("febeUDBuDesc").disabled = false;
  253.     document.getElementById("febeUDBuSourceID").disabled = false;
  254.     document.getElementById("febeUDBuSource").disabled = false;
  255.     document.getElementById("febeUDBuBrowseBtn").disabled = false;
  256.     document.getElementById("febeIncludeInBu_id").disabled = false;
  257.     document.getElementById("febeUDBuSubmitBtn").disabled = false;
  258.     document.getElementById("febeUDBuLabel").value = "";
  259.     document.getElementById("febeUDBuDesc").value = "";
  260.     document.getElementById("febeUDBuSource").value = "";
  261.     document.getElementById("febeIncludeInBu_id").checked = false;
  262.     return true;
  263. }//febeUDBuEnableNew()
  264.  
  265. function febeUDBuEnableEdit(){
  266.     document.getElementById("febeUDBuList").removeAllItems();
  267.     document.getElementById("febeUDBuList").setAttribute("label",febeMsg[135]);
  268.     document.getElementById("febeUDBuList").disabled = false;
  269.     document.getElementById("febeUDBuLabelID").disabled = false;
  270.     document.getElementById("febeUDBuLabel").disabled = true;
  271.     document.getElementById("febeUDBuTypeID").disabled = false;
  272.     document.getElementById("febeUDBuType").disabled = false;
  273.     document.getElementById("febeUDBuDescID").disabled = false;
  274.     document.getElementById("febeUDBuDesc").disabled = false;
  275.     document.getElementById("febeUDBuSourceID").disabled = false;
  276.     document.getElementById("febeUDBuSource").disabled = false;
  277.     document.getElementById("febeUDBuBrowseBtn").disabled = false;
  278.     document.getElementById("febeIncludeInBu_id").disabled = false;
  279.     document.getElementById("febeUDBuSubmitBtn").disabled = false;
  280.     document.getElementById("febeUDBuLabel").value = "";
  281.     document.getElementById("febeUDBuDesc").value = "";
  282.     document.getElementById("febeUDBuSource").value = "";
  283.     document.getElementById("febeIncludeInBu_id").checked = false;
  284.     return true;
  285. }//febeUDBuEnableEdit()
  286.  
  287. function febeUDBuEnableDelete(){
  288.     //document.getElementById("febeUDBuCaption").value = febeMsg[138];
  289.     document.getElementById("febeUDBuList").removeAllItems();
  290.     document.getElementById("febeUDBuList").setAttribute("label",febeMsg[135]);
  291.     document.getElementById("febeUDBuList").disabled = false;
  292.     document.getElementById("febeUDBuLabelID").disabled = false;
  293.     document.getElementById("febeUDBuLabel").disabled = true;
  294.     document.getElementById("febeUDBuTypeID").disabled = true;
  295.     document.getElementById("febeUDBuType").disabled = true;
  296.     document.getElementById("febeUDBuDescID").disabled = false;
  297.     document.getElementById("febeUDBuDesc").disabled = true;
  298.     document.getElementById("febeUDBuSourceID").disabled = false;
  299.     document.getElementById("febeUDBuSource").disabled = true;
  300.     document.getElementById("febeUDBuBrowseBtn").disabled = true;
  301.     document.getElementById("febeIncludeInBu_id").disabled = true;
  302.     document.getElementById("febeUDBuSubmitBtn").disabled = false;
  303.     document.getElementById("febeUDBuLabel").value = "";
  304.     document.getElementById("febeUDBuDesc").value = "";
  305.     document.getElementById("febeUDBuSource").value = "";
  306.     document.getElementById("febeIncludeInBu_id").checked = false;
  307.     return true;
  308. }//febeUDBuEnableDelete()
  309.  
  310. function febeUDBuOK(){
  311.     febeUDBuWrite();
  312.     febeAlert(febeMsg[149]);
  313.     return true;
  314. }//febeUDBuOK()
  315.  
  316. function febeUDBuCancel(){
  317.     if(febeUDBuTrxPending == true){
  318.         var style = "<style>color: red; font-weight: bold; font-size: 20px;</style>"
  319.         var tmp = style+febeMsg[139]+"\n";
  320.         tmp += style+febeMsg[140]+"\n\n";
  321.         tmp += febeMsg[141];
  322.         return febeConfirm(tmp);
  323.     }//if
  324.     return true;
  325. }//febeUDBuCancel()
  326.  
  327. function febeUDBuSubmit(){
  328.     var febeUDBuItem = new febeUDBuObj; 
  329.     febeUDBuItem.Label = document.getElementById("febeUDBuLabel").value;
  330.     febeUDBuItem.Type = document.getElementById("febeUDBuType").selectedIndex;
  331.     febeUDBuItem.Description = document.getElementById("febeUDBuDesc").value;
  332.     febeUDBuItem.Path = document.getElementById("febeUDBuSource").value;
  333.     febeUDBuItem.Include = document.getElementById("febeIncludeInBu_id").checked;
  334.     var key = febeUDBuItem.Label;
  335.     switch(febeUDBuAction){
  336.         case 1:    // New
  337.             if(febeUDBuItem.Label == ""){
  338.                 febeAlert(febeMsg[142]);
  339.                 return false;
  340.             }//if
  341.             if(febeUDBuItem.Type == -1){
  342.                 febeAlert(febeMsg[160]);
  343.                 return false;
  344.             }//if
  345.             if(febeUDBuItem.Description == ""){
  346.                 febeAlert(febeMsg[143]);
  347.                 return false;
  348.             }//if
  349.             if(febeUDBuItem.Path == ""){
  350.                 febeAlert(febeMsg[144]);
  351.                 return false;
  352.             }//if
  353.             // See if label already exists
  354.             for(var tmp in febeUDBuList){
  355.                 if(tmp == key){
  356.                     febeAlert(febeMsg[145].replace("%key%",key));
  357.                     return false;
  358.                 }//if
  359.             }//for
  360.             febeUDBuList[key] = febeUDBuItem;
  361.             break;
  362.         case 2:    // Edit
  363.             febeUDBuList[key] = febeUDBuItem;
  364.             break;
  365.         case 3:    // Delete
  366.             delete febeUDBuList[key];
  367.             break;
  368.         default:// WTF??
  369.         ;
  370.     }//switch
  371.     febeUDBuTrxPending = true;
  372.     document.documentElement.getButton("extra2").disabled = false;
  373.     febeUDBuDisableAll();
  374.     febeAlert(febeMsg[146]);
  375.     return true;
  376. }//febeUDBuSubmit()
  377.  
  378. function febeUDBuPopulateListbox(){
  379.     // Clear existing items
  380.     var submenu = document.getElementById("febeUDBuList");
  381.     var children = submenu.childNodes;
  382.     var n = children.length;
  383.     for (var i = 0; i < n; i++) {
  384.         submenu.removeChild(children[0]);
  385.     }
  386.     for(var i in febeUDBuList){
  387.         document.getElementById("febeUDBuList").appendItem(febeUDBuList[i].Label,febeUDBuList[i].Label);
  388.     }//for
  389.     document.getElementById("febeUDBuList").disabled = false;
  390.     return true;
  391. }//febeUDBuPopulateListbox()
  392.  
  393. function febeUDBuDisplayData(){
  394.     switch(febeUDBuAction){
  395.         case 2: 
  396.             febeUDBuEnableEdit();
  397.             break;
  398.         case 3:
  399.             febeUDBuEnableDelete();
  400.             break;
  401.     }//switch
  402.     var key = document.getElementById("febeUDBuList").value;
  403.     document.getElementById("febeUDBuLabel").value = febeUDBuList[key].Label;
  404.     document.getElementById("febeUDBuType").selectedIndex = febeUDBuList[key].Type;
  405.     document.getElementById("febeUDBuDesc").value = febeUDBuList[key].Description;
  406.     document.getElementById("febeUDBuSource").value = febeUDBuList[key].Path;
  407.     document.getElementById("febeIncludeInBu_id").setAttribute("checked",febeUDBuList[key].Include);
  408.     return true;
  409. }//febeUDBuDisplayData()
  410.  
  411. function febe_fillUDBUmenu(element){
  412.   // Populate the restore UDBU submenu
  413.   
  414.     // Clear existing items
  415.     var submenu = document.getElementById(element);
  416.     var children = submenu.childNodes;
  417.     var n = children.length;
  418.     for (var i = 0; i < n; i++) {
  419.         submenu.removeChild(children[0]);
  420.     }
  421.     febeUDBuInit();
  422.     for(var i in febeUDBuList){
  423.         var oncommand = "febeRestoreUDBU('"+i+"');";
  424.         var aMenuItem=document.createElement("menuitem");
  425.         aMenuItem.setAttribute( "label" , febeUDBuList[i].Label);
  426.         aMenuItem.setAttribute( "oncommand" , oncommand);
  427.         aMenuItem.setAttribute( "tooltiptext" , febeMsg[170]+" "+febeUDBuList[i].Description);
  428.         document.getElementById(element).appendChild(aMenuItem);
  429.     }//for
  430.     return true;
  431. }//febe_fillUDBUmenu()
  432.  
  433. function febeResetUDBu(){
  434.     var style = "<style>color: red; font-weight: bold; font-size: 14px;</style>"
  435.     var tmp = style+febeMsg[174]+"\n\n";
  436.     tmp += febeMsg[175];
  437.     if(!febeConfirm(tmp)){return true;};
  438.     var febeDataFile = Components.classes["@mozilla.org/file/directory_service;1"]
  439.                  .getService(Components.interfaces.nsIProperties)
  440.                  .get("ProfD", Components.interfaces.nsIFile);
  441.     febeDataFile.append(FEBEDATAFILE);
  442.     if(febeDataFile.exists()){febeDataFile.remove(false);}
  443.     febeUDBuList = [];
  444.     febeUDBuInit();
  445.     febeUDBuPopulateListbox();
  446.     febeUDBuDisableAll();
  447.     febeAlert(febeMsg[176]);
  448.     return true;
  449. }//febeResetUDBu()
  450.  
  451. //febeAlert("febeDBM.js loaded");